A semi-global matching (SGM) is a type of stereo matching methods of obtaining distance information from two or more images. There is an image processing apparatus which stores intermediate data including information calculated for each pixel of an input image during steps until a corresponding point with the pixel of the input image is searched from a reference image. In the case of the SGM, the image processing apparatus obtains a cost of a disparity using information of a target pixel in the input image, and information of a periphery of the target pixel. In the SGM, a size of the intermediate data to be transferred and held between steps is large.